package controller;

import cn.hutool.json.JSONUtil;
import entity.User;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;

/**
 * Author：汤小洋
 * Date：2023-09-20 16:39
 * Description：<描述>
 */
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(){
        System.out.println("HelloController.hello.......................");

        ModelAndView mav = new ModelAndView();
        mav.setViewName("hello");
        mav.addObject("name","张三");
        return mav; // 拼接 /view/hello.jsp
    }

    @RequestMapping("/hello2")
    public String hello2(){
        System.out.println("HelloController.hello2.....................");
        // return "hello";
        // return "forward:/hello"; // 转发到指定的url
        return "redirect:/hello"; // 重定向到指定的url
    }


    @SneakyThrows
    @RequestMapping("/hello3")
    public void hello3(HttpServletResponse response)  {
        System.out.println("HelloController.hello3.....................");

        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        User user = new User(1,"张三","123",18,192.4, LocalDateTime.now());
        out.print(JSONUtil.toJsonStr(user));
        out.close();
    }

}
